﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using Windows8Accelerator.Core.Xaml.Controls;

namespace Windows8Accelerator.Core.Xaml.Helpers
{
    public class SuperTextFormatHelper : SuperTextFormatHelperBase
    {
        public override void Format(SuperTextFormat textFormat, object control)
        {
            if (control is Control)
                FormatControl(textFormat, (Control)control);
            else if (control is TextElement)
                FormatTextElement(textFormat, (TextElement)control);
        }

        public override SuperTextFormat GetFormat(object control)
        { 
            if (control is Control)
                return GetControlFormat((Control)control);
            else if (control is TextElement)
                return GetTextElementFormat((TextElement)control);
            return  null;
        }

        public static void FormatTextElement(SuperTextFormat textFormat, TextElement textElement)
        {
            if (textFormat != null)
            {
                if (textFormat.FontWeight.HasValue)
                    textElement.FontWeight = ConvertToFontWeight(textFormat.FontWeight.Value);
                if (textFormat.FontSize.HasValue)
                    textElement.FontSize = textFormat.FontSize.Value;
                if (textFormat.FontStyle.HasValue)
                    textElement.FontStyle = ConvertToFontStyle(textFormat.FontStyle.Value);
                if (!string.IsNullOrEmpty(textFormat.FontFamilyName))
                    textElement.FontFamily = new FontFamily(textFormat.FontFamilyName);
                if (!string.IsNullOrEmpty(textFormat.ForegroundColor))
                    textElement.Foreground = new SolidColorBrush(ColorHelperEx.StringToColor(textFormat.ForegroundColor));
            }
        }

        public static void FormatControl(SuperTextFormat textFormat, Control control)
        {
            if (textFormat != null)
            {
                if (textFormat.FontWeight.HasValue)
                    control.FontWeight = ConvertToFontWeight(textFormat.FontWeight.Value);
                if (textFormat.FontSize.HasValue)
                    control.FontSize = textFormat.FontSize.Value;
                if (textFormat.FontStyle.HasValue)
                    control.FontStyle = ConvertToFontStyle(textFormat.FontStyle.Value);
                if (!string.IsNullOrEmpty(textFormat.FontFamilyName))
                    control.FontFamily = new FontFamily(textFormat.FontFamilyName);
                if (!string.IsNullOrEmpty(textFormat.ForegroundColor))
                    control.Foreground = new SolidColorBrush(ColorHelperEx.StringToColor(textFormat.ForegroundColor));
            }
        }

        public static SuperTextFormat GetControlFormat(Control control)
        {
            return new SuperTextFormat() { 
                FontFamilyName = control.FontFamily.Source,
                FontSize = control.FontSize,
                FontStyle = ConvertFromFontStyle(control.FontStyle),
                FontWeight = ConvertFromFontWeight(control.FontWeight),
                ForegroundColor =  ColorHelperEx.ColorToString(((SolidColorBrush)control.Foreground).Color),
            };
        }

        public static SuperTextFormat GetTextElementFormat(TextElement control)
        {
            return new SuperTextFormat()
            {
                FontFamilyName = control.FontFamily.Source,
                FontSize = control.FontSize,
                FontStyle = ConvertFromFontStyle(control.FontStyle),
                FontWeight = ConvertFromFontWeight(control.FontWeight),
                ForegroundColor = ColorHelperEx.ColorToString(((SolidColorBrush)control.Foreground).Color),
            };
        }

        public static FontStyle ConvertToFontStyle(SuperFontStyle superFontStyle)
        {
            switch (superFontStyle)
            {
                case SuperFontStyle.Italic:
                    return FontStyles.Italic;
                case SuperFontStyle.Normal:
                default:
                    return FontStyles.Normal;
            }
        }

        public static FontWeight ConvertToFontWeight(SuperFontWeight superFontWeight)
        {
            switch (superFontWeight)
            {
                case SuperFontWeight.Black:
                    return FontWeights.Black;
                case SuperFontWeight.Bold:
                    return FontWeights.Bold;
                case SuperFontWeight.ExtraBlack:
                    return FontWeights.ExtraBlack;
                case SuperFontWeight.ExtraBold:
                    return FontWeights.ExtraBold;
                case SuperFontWeight.ExtraLight:
                    return FontWeights.ExtraLight;
                case SuperFontWeight.Light:
                    return FontWeights.Light;
                case SuperFontWeight.Medium:
                    return FontWeights.Medium;
                case SuperFontWeight.Normal:
                default:
                    return FontWeights.Normal;
                case SuperFontWeight.SemiBold:
                    return FontWeights.SemiBold;
                case SuperFontWeight.Thin:
                    return FontWeights.Thin;
            }
        }

        public static SuperFontStyle ConvertFromFontStyle(FontStyle fontStyle)
        {
            if (fontStyle == FontStyles.Italic)
                return SuperFontStyle.Italic;
            else if (fontStyle == FontStyles.Normal)
                return SuperFontStyle.Normal;
            return SuperFontStyle.Normal;
        }

        public static SuperFontWeight ConvertFromFontWeight(FontWeight fontWeight)
        {
            if (fontWeight == FontWeights.Black)
                return SuperFontWeight.Black;
            else if (fontWeight == FontWeights.Bold)
                return SuperFontWeight.Bold;
            else if (fontWeight == FontWeights.ExtraBlack)
                return SuperFontWeight.ExtraBlack;
            else if (fontWeight == FontWeights.ExtraBold)
                return SuperFontWeight.ExtraBold;
            else if (fontWeight == FontWeights.ExtraLight)
                return SuperFontWeight.ExtraLight;
            else if (fontWeight == FontWeights.Light)
                return SuperFontWeight.Light;
            else if (fontWeight == FontWeights.Medium)
                return SuperFontWeight.Medium;
            else if (fontWeight == FontWeights.Normal)
                return SuperFontWeight.Normal;
            else if (fontWeight == FontWeights.SemiBold)
                return SuperFontWeight.SemiBold;
            else if (fontWeight == FontWeights.Thin)
                return SuperFontWeight.Thin;
            return SuperFontWeight.Normal;
        }

    }
}
